package com.jwl.idc.customview;

import com.jwl.idc.R;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomDialog extends Dialog {

	private Context context;
	private String title;
	private String message;
	private String positiveButtonText;
	private String negativeButtonText;
	private boolean isHasBottom;
	private View contentView;
	private int dialogId;

	public int getDialogId() {
		return dialogId;
	}

	public void setDialogId(int dialogId) {
		this.dialogId = dialogId;
	}

	private OnClickListener positiveButtonClickListener,
			negativeButtonClickListener;

	public CustomDialog(Context context, int theme) {
		super(context, theme);
		context = context;
	}

	public CustomDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		context = context;
	}

	public CustomDialog create(Context context) {
		if (context == null) {
		}
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final CustomDialog dialog = new CustomDialog(context,
				R.style.CustomDialog);
		View layout = inflater.inflate(R.layout.custom_dialog, null);
		dialog.addContentView(layout, new LayoutParams(
				LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
		if (null == title || "".equals(title)) {
			((LinearLayout) layout.findViewById(R.id.cd_title))
					.setVisibility(View.GONE);
		} else {
			((TextView) layout.findViewById(R.id.title)).setText(title);
		}

		if (isHasBottom) {
			// ((LinearLayout)layout.findViewById(R.id.cd_bottom)).setBackgroundColor(context.getResources().getColor(R.color.transparent));
			((LinearLayout) layout.findViewById(R.id.cd_bottom))
					.setVisibility(View.VISIBLE);
		} else {
			((LinearLayout) layout.findViewById(R.id.cd_bottom))
					.setVisibility(View.GONE);
		}

		if (positiveButtonText != null) {
			((Button) layout.findViewById(R.id.positiveButton))
					.setText(positiveButtonText);
			if (positiveButtonClickListener != null) {
				((Button) layout.findViewById(R.id.positiveButton))
						.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								positiveButtonClickListener.onClick(dialog,
										DialogInterface.BUTTON_POSITIVE);
							}
						});
			}
		} else {
			layout.findViewById(R.id.positiveButton).setVisibility(View.GONE);
			layout.findViewById(R.id.zjxIV).setVisibility(View.GONE);
		}
		if (negativeButtonText != null) {
			((Button) layout.findViewById(R.id.negativeButton))
					.setText(negativeButtonText);
			if (negativeButtonClickListener != null) {
				((Button) layout.findViewById(R.id.negativeButton))
						.setOnClickListener(new View.OnClickListener() {
							public void onClick(View v) {
								negativeButtonClickListener.onClick(dialog,
										DialogInterface.BUTTON_NEGATIVE);
							}
						});
			}
		} else {
			layout.findViewById(R.id.negativeButton).setVisibility(View.GONE);
		}


		if (message != null) {
			((TextView) layout.findViewById(R.id.message)).setText(message);
		} else if (contentView != null) {
			((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
			((LinearLayout) layout.findViewById(R.id.content)).addView(
					contentView, new LayoutParams(LayoutParams.MATCH_PARENT,
							LayoutParams.WRAP_CONTENT));
		}
		dialog.setContentView(layout);

		return dialog;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getPositiveButtonText() {
		return positiveButtonText;
	}

	public void setPositiveButtonText(String positiveButtonText) {
		this.positiveButtonText = positiveButtonText;
	}

	public String getNegativeButtonText() {
		return negativeButtonText;
	}

	public void setNegativeButtonText(String negativeButtonText) {
		this.negativeButtonText = negativeButtonText;
	}

	public boolean isHasBottom() {
		return isHasBottom;
	}

	public void setHasBottom(boolean isHasBottom) {
		this.isHasBottom = isHasBottom;
	}

	public View getContentView() {
		return contentView;
	}

	public void setContentView(View contentView) {
		this.contentView = contentView;
	}

	public OnClickListener getPositiveButtonClickListener() {
		return positiveButtonClickListener;
	}

	public void setPositiveButtonClickListener(
			OnClickListener positiveButtonClickListener) {
		this.positiveButtonClickListener = positiveButtonClickListener;
	}

	public OnClickListener getNegativeButtonClickListener() {
		return negativeButtonClickListener;
	}

	public void setNegativeButtonClickListener(
			OnClickListener negativeButtonClickListener) {
		this.negativeButtonClickListener = negativeButtonClickListener;
	}

}
